<template>
	<view>
		<view class="feedback-title">
			意见反馈:
		</view>
		<view class="feedback-content">
			<textarea class="feedback-textarea" v-model="content" placeholder="请输出您要反馈的问题" />
		</view>

		<view class="feedback-title">
			反馈图片（图片上限为6张）:
		</view>
		<view class="feedback-image-box">
			<view class="feedback-image-item" v-for="(item,index) in imageList" :key="index">
				<view class="close-icon" @click="del(index)">
					<uni-icons type="closeempty" size="18" color="#fff"></uni-icons>
				</view>
				<view class="image-box">
					<image :src="item.url" mode="aspectFill"></image>
				</view>
			</view>
			<!-- 添加框 -->
			<view class="feedback-image-item" @click="addImage" v-if="imageList.length<6">
				<view class="image-box">
					<uni-icons type="plusempty" size="50" color="#eee"></uni-icons>
				</view>
			</view>
		</view>
		<button type="primary" class="feedback-btn" @click="submit">提交反馈意见</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				imageList: [],
				content:''
			}
		},
		methods: {
			// 图片添加
			addImage() {
				const count = 6 - this.imageList.length
				uni.chooseImage({
					count: count, //对h5端无效
					success: (res) => {

						const tempFilePaths = res.tempFilePaths
						tempFilePaths.forEach((item, index) => {
							if (index < count) {
								this.imageList.push({
									url: item
								})
							}
						})
						console.log(this.imageList)
					}
				})
			},
			// 删除图片
			del(index) {
				this.imageList.splice(index, 1)
			},
			//提交键 上传图片
			async submit() {
				uni.showLoading()
				let imagePath = []
				for (let i = 0; i < this.imageList.length; i++) {
					let filePath = this.imageList[i].url
					await this.uploadFiles(filePath)
					imagePath.push(filePath)
				}
				// console.log(imagePath)
				this.setUpdateFeedback(imagePath,this.content)
			},
			async uploadFiles(filePath) {
				const dir = "feedback/"
				let result = await uniCloud.uploadFile({
					filePath: filePath,
					cloudPath: dir + filePath,
					// fileType: "image",
				})
				// console.log(result)
				return result.fileID
			},
			setUpdateFeedback(feedbackImage,content){
				this.$api.update_feedback({
					feedbackImage,
					content
				}).then((res)=>{
					uni.hideLoading()
					uni.showToast({
						title:'提交成功',
						icon:'none'
					})
					setTimeout(()=>{
						this.content=''
						this.imageList=[]
					},1000)
				}).catch((err)=>{
					uni.hideLoading()
					uni.showToast({
						title:'提交失败',
						icon:'none'
					})
				})
			}
		}
	}
</script>

<style lang="scss">
	.feedback-title {
		margin: 15px 15px 0 15px;
		color: #666;
		font-size: 14px;
	}

	.feedback-content {
		box-sizing: border-box;
		margin: 15px;
		padding: 10px;
		border: 1px #eee solid;

		.feedback-textarea {
			width: 100%;
			height: 100px;
			font-size: 14px;
		}
	}

	.feedback-image-box {
		display: flex;
		flex-wrap: wrap;
		padding: 10px;

		.feedback-image-item {
			box-sizing: border-box;
			position: relative;
			// 正方操作width = padding-top
			height: 0;
			width: 33.3%;
			padding-top: 33.3%;

			.close-icon {
				background-color: #ff5a5f;
				width: 22px;
				height: 22px;
				position: absolute;
				top: 0;
				right: 0;
				z-index: 2;
				border-radius: 50%;
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.image-box {
				display: flex;
				justify-content: center;
				align-items: center;
				position: absolute;
				top: 5px;
				right: 5px;
				left: 5px;
				bottom: 5px;
				overflow: hidden;
				border-radius: 5px;

				border: 1px #ccc solid;

				image {
					width: 100%;
					height: 100%;
				}
			}
		}
	}

	.feedback-btn {
		margin: 0 15px;
		background-color: $mk-base-color;
	}
</style>
